package com.kaili.tms.controller.web.v1;

import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.tms.pojo.dto.SchedulingPlanDetailsAddDTO;
import com.kaili.tms.pojo.dto.SchedulingPlanDetailsUpdateDTO;
import com.kaili.tms.pojo.vo.BaseDriverVO;
import com.kaili.tms.pojo.vo.DateVO;
import com.kaili.tms.service.SchedulingPlanService;
import com.kaili.tms.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @version 1.0
 * @author: xinglei
 * @Time: 19-1-29 下午2:31
 * @Copyright: © 2018 杭州凯立通信有限公司 版权所有
 * @Warning: 本内容仅限于公司内部传阅, 禁止外泄或用于其它商业目的
 */
@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "排班计划管理明细", description = "排班计划管理明细API")
public class SchedulingPlanDetailController {

    @Autowired
    private SchedulingPlanService schedulingPlanService;

    @PostMapping(value = "/schedulingPlanDetails")
    @ApiOperation(value = "增加排班明细")
    @Transactional
    public RestStatus schedulingPlanDetailAdd(@RequestBody SchedulingPlanDetailsAddDTO schedulingPlanDetailsAddDTO) {
        Integer status = schedulingPlanService.shiftsSchedulingAdd(schedulingPlanDetailsAddDTO);
        if (status == 1) {
            return CommonStatus.ADD_FAIL;
        }
        return CommonStatus.ADD_OK;
    }

    @PutMapping(value = "/schedulingPlanDetails")
    @ApiOperation(value = "修改排班明细")
    public RestStatus schedulingPlanDetailUpdate(@RequestBody List<SchedulingPlanDetailsUpdateDTO> schedulingPlanDetailsUpdateDTO) {
        schedulingPlanService.schedulingPlanDetailUpdate(schedulingPlanDetailsUpdateDTO);
        return CommonStatus.UPDATE_OK;
    }

  /* @GetMapping(value = "/schedulingPlansId")
  @ApiOperation(value = "根据车线ID获取班次维护规则")
  public List<ShiftsRulesVO> schedulingPlanGetAll(@RequestParam("carLineId") Long carLineId) {
      return schedulingPlanService.getAllShiftsRules(carLineId);
  }*/

    @GetMapping(value = "/schedulingPlans/like")
    @ApiOperation(value = "根据车线ID模糊查询车牌号")
    public List<BaseDriverVO> schedulingPlanCarId(@RequestParam("carNum") String carNum, @RequestParam("carLineId") Long carLineId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("carNum", carNum);
        map.put("carLineId", carLineId);
        return schedulingPlanService.schedulingPlanCarId(map);
    }

    @GetMapping(value = "/schedulingPlans/random")
    @ApiOperation(value = "随机排班")
    public List<DateVO> schedulingPlansRandom(
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime,
            @RequestParam("carLineId") Long carLineId,
            @RequestParam("planId") Long planId) {
        List<DateVO> dateVO =
                schedulingPlanService.getDateAndschedulingPlanRanom(startTime, endTime, carLineId, planId);
        // return schedulingPlanService.schedulingPlanCarId(map);

        return dateVO;
    }

    @GetMapping(value = "/schedulingPlans/date")
    @ApiOperation(value = "时间转换+查询班次规则")
    public List<DateVO> getDate(@RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime,
                                @RequestParam("carLineId") Long carLineId, @RequestParam("planId") Long planId) {
        //分割时间和明细数据
        List<DateVO> dateVO = schedulingPlanService.getDateAndschedulingPlan(startTime, endTime, carLineId, planId);
        return dateVO;
    }

    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dBegin = sdf.parse("2017-03-03");
            Date dEnd = sdf.parse("2017-06-04");
            List<DateVO> datas = findDates1(dBegin, dEnd);
            System.out.println(datas);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public static List<DateVO> findDates1(Date dBegin, Date dEnd) {
        List<DateVO> lDate = new ArrayList<DateVO>();
        // List<DateVO> lDate1 = new ArrayList<DateVO>();
        SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd");
        DateVO dateVO = new DateVO();
        dateVO.setDate(sd.format(dBegin));
        lDate.add(dateVO);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后
        DateVO dateVO1;
        while (dEnd.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            dateVO1 = new DateVO();
            dateVO1.setDate(sd.format(calBegin.getTime()));
            lDate.add(dateVO1);
        }
        return lDate;
    }
}
